﻿using CKS.Core.Framework;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP60
{
    /// <summary>
    /// 盖板底座焊丝组合
    /// </summary>
    public class DB3031_Work01Action : PLCAction
    {
        public DB3031_Work01Action(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            var result = Value.Value.ToInt();
            if (result == 1)
            {

                DcOp60Record dc = new DcOp60Record()
                {
                    CreatedTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    WeldingWireBasketCode = Value.GetLinkData(1).ToString(),
                    CoverCode = Value.GetLinkData(2).ToString(),
                    BottomPlateCode = Value.GetLinkData(3).ToString(),
                    WeldingWireSide = Value.GetLinkData(4).ToInt(),
                };
                 db.Insertable(dc).ExecuteCommand();

                //保存数据
                //读取子篮二维码和检测结果
                //string barcode = Value.GetLinkData(1).ToString();
                //int flag = Value.GetLinkData(2).ToInt();

                //logger.LogInformation($"保存数据 子篮码{barcode} 检测结果{flag}");
                //查找保存完成点位
               
                var replayPos = Value.GetPosition(3030, 1);
                AckWriteInt16(replayPos, 1);
                EventBus.AddLog($"OP60请求保存数据 {dc.CoverCode} {dc.BottomPlateCode}");
            }
            else
            {
                var replayPos = Value.GetPosition(3030, 1);
                AckWriteInt16(replayPos, 0);
              
                EventBus.AddLog($"OP60加工结果-重置保存状态");
            }
        }
    }
}
